-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
kmp_affinity.cpp
4735 lines (4254 loc) · 157 KB
/
kmp_affinity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* kmp_affinity.cpp -- affinity management
*/
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//
#include "kmp.h"
#include "kmp_i18n.h"
#include "kmp_io.h"
#include "kmp_str.h"
#include "kmp_wrapper_getpid.h"
#include "kmp_affinity.h"
// Store the real or imagined machine hierarchy here
static hierarchy_info machine_hierarchy;
void __kmp_cleanup_hierarchy() {
machine_hierarchy.fini();
}
void __kmp_get_hierarchy(kmp_uint32 nproc, kmp_bstate_t *thr_bar) {
kmp_uint32 depth;
// The test below is true if affinity is available, but set to "none". Need to init on first use of hierarchical barrier.
if (TCR_1(machine_hierarchy.uninitialized))
machine_hierarchy.init(NULL, nproc);
// Adjust the hierarchy in case num threads exceeds original
if (nproc > machine_hierarchy.base_num_threads)
machine_hierarchy.resize(nproc);
depth = machine_hierarchy.depth;
KMP_DEBUG_ASSERT(depth > 0);
thr_bar->depth = depth;
thr_bar->base_leaf_kids = (kmp_uint8)machine_hierarchy.numPerLevel[0]-1;
thr_bar->skip_per_level = machine_hierarchy.skipPerLevel;
}
#if KMP_AFFINITY_SUPPORTED
//
// Print the affinity mask to the character array in a pretty format.
//
#if KMP_USE_HWLOC
char *
__kmp_affinity_print_mask(char *buf, int buf_len, kmp_affin_mask_t *mask)
{
int num_chars_to_write, num_chars_written;
char* scan;
KMP_ASSERT(buf_len >= 40);
// bufsize of 0 just retrieves the needed buffer size.
num_chars_to_write = hwloc_bitmap_list_snprintf(buf, 0, (hwloc_bitmap_t)mask);
// need '{', "xxxxxxxx...xx", '}', '\0' = num_chars_to_write + 3 bytes
// * num_chars_to_write returned by hwloc_bitmap_list_snprintf does not
// take into account the '\0' character.
if(hwloc_bitmap_iszero((hwloc_bitmap_t)mask)) {
KMP_SNPRINTF(buf, buf_len, "{<empty>}");
} else if(num_chars_to_write < buf_len - 3) {
// no problem fitting the mask into buf_len number of characters
buf[0] = '{';
// use buf_len-3 because we have the three characters: '{' '}' '\0' to add to the buffer
num_chars_written = hwloc_bitmap_list_snprintf(buf+1, buf_len-3, (hwloc_bitmap_t)mask);
buf[num_chars_written+1] = '}';
buf[num_chars_written+2] = '\0';
} else {
// Need to truncate the affinity mask string and add ellipsis.
// To do this, we first write out the '{' + str(mask)
buf[0] = '{';
hwloc_bitmap_list_snprintf(buf+1, buf_len-7, (hwloc_bitmap_t)mask);
// then, what we do here is go to the 7th to last character, then go backwards until we are NOT
// on a digit then write "...}\0". This way it is a clean ellipsis addition and we don't
// overwrite part of an affinity number. i.e., we avoid something like { 45, 67, 8...} and get
// { 45, 67,...} instead.
scan = buf + buf_len - 7;
while(*scan >= '0' && *scan <= '9' && scan >= buf)
scan--;
*(scan+1) = '.';
*(scan+2) = '.';
*(scan+3) = '.';
*(scan+4) = '}';
*(scan+5) = '\0';
}
return buf;
}
#else
char *
__kmp_affinity_print_mask(char *buf, int buf_len, kmp_affin_mask_t *mask)
{
KMP_ASSERT(buf_len >= 40);
char *scan = buf;
char *end = buf + buf_len - 1;
//
// Find first element / check for empty set.
//
size_t i;
for (i = 0; i < KMP_CPU_SETSIZE; i++) {
if (KMP_CPU_ISSET(i, mask)) {
break;
}
}
if (i == KMP_CPU_SETSIZE) {
KMP_SNPRINTF(scan, end-scan+1, "{<empty>}");
while (*scan != '\0') scan++;
KMP_ASSERT(scan <= end);
return buf;
}
KMP_SNPRINTF(scan, end-scan+1, "{%ld", (long)i);
while (*scan != '\0') scan++;
i++;
for (; i < KMP_CPU_SETSIZE; i++) {
if (! KMP_CPU_ISSET(i, mask)) {
continue;
}
//
// Check for buffer overflow. A string of the form ",<n>" will have
// at most 10 characters, plus we want to leave room to print ",...}"
// if the set is too large to print for a total of 15 characters.
// We already left room for '\0' in setting end.
//
if (end - scan < 15) {
break;
}
KMP_SNPRINTF(scan, end-scan+1, ",%-ld", (long)i);
while (*scan != '\0') scan++;
}
if (i < KMP_CPU_SETSIZE) {
KMP_SNPRINTF(scan, end-scan+1, ",...");
while (*scan != '\0') scan++;
}
KMP_SNPRINTF(scan, end-scan+1, "}");
while (*scan != '\0') scan++;
KMP_ASSERT(scan <= end);
return buf;
}
#endif // KMP_USE_HWLOC
void
__kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask)
{
KMP_CPU_ZERO(mask);
# if KMP_GROUP_AFFINITY
if (__kmp_num_proc_groups > 1) {
int group;
KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
for (group = 0; group < __kmp_num_proc_groups; group++) {
int i;
int num = __kmp_GetActiveProcessorCount(group);
for (i = 0; i < num; i++) {
KMP_CPU_SET(i + group * (CHAR_BIT * sizeof(DWORD_PTR)), mask);
}
}
}
else
# endif /* KMP_GROUP_AFFINITY */
{
int proc;
for (proc = 0; proc < __kmp_xproc; proc++) {
KMP_CPU_SET(proc, mask);
}
}
}
//
// When sorting by labels, __kmp_affinity_assign_child_nums() must first be
// called to renumber the labels from [0..n] and place them into the child_num
// vector of the address object. This is done in case the labels used for
// the children at one node of the hierarchy differ from those used for
// another node at the same level. Example: suppose the machine has 2 nodes
// with 2 packages each. The first node contains packages 601 and 602, and
// second node contains packages 603 and 604. If we try to sort the table
// for "scatter" affinity, the table will still be sorted 601, 602, 603, 604
// because we are paying attention to the labels themselves, not the ordinal
// child numbers. By using the child numbers in the sort, the result is
// {0,0}=601, {0,1}=603, {1,0}=602, {1,1}=604.
//
static void
__kmp_affinity_assign_child_nums(AddrUnsPair *address2os,
int numAddrs)
{
KMP_DEBUG_ASSERT(numAddrs > 0);
int depth = address2os->first.depth;
unsigned *counts = (unsigned *)__kmp_allocate(depth * sizeof(unsigned));
unsigned *lastLabel = (unsigned *)__kmp_allocate(depth
* sizeof(unsigned));
int labCt;
for (labCt = 0; labCt < depth; labCt++) {
address2os[0].first.childNums[labCt] = counts[labCt] = 0;
lastLabel[labCt] = address2os[0].first.labels[labCt];
}
int i;
for (i = 1; i < numAddrs; i++) {
for (labCt = 0; labCt < depth; labCt++) {
if (address2os[i].first.labels[labCt] != lastLabel[labCt]) {
int labCt2;
for (labCt2 = labCt + 1; labCt2 < depth; labCt2++) {
counts[labCt2] = 0;
lastLabel[labCt2] = address2os[i].first.labels[labCt2];
}
counts[labCt]++;
lastLabel[labCt] = address2os[i].first.labels[labCt];
break;
}
}
for (labCt = 0; labCt < depth; labCt++) {
address2os[i].first.childNums[labCt] = counts[labCt];
}
for (; labCt < (int)Address::maxDepth; labCt++) {
address2os[i].first.childNums[labCt] = 0;
}
}
}
//
// All of the __kmp_affinity_create_*_map() routines should set
// __kmp_affinity_masks to a vector of affinity mask objects of length
// __kmp_affinity_num_masks, if __kmp_affinity_type != affinity_none, and
// return the number of levels in the machine topology tree (zero if
// __kmp_affinity_type == affinity_none).
//
// All of the __kmp_affinity_create_*_map() routines should set *fullMask
// to the affinity mask for the initialization thread. They need to save and
// restore the mask, and it could be needed later, so saving it is just an
// optimization to avoid calling kmp_get_system_affinity() again.
//
static kmp_affin_mask_t *fullMask = NULL;
kmp_affin_mask_t *
__kmp_affinity_get_fullMask() { return fullMask; }
static int nCoresPerPkg, nPackages;
static int __kmp_nThreadsPerCore;
#ifndef KMP_DFLT_NTH_CORES
static int __kmp_ncores;
#endif
//
// __kmp_affinity_uniform_topology() doesn't work when called from
// places which support arbitrarily many levels in the machine topology
// map, i.e. the non-default cases in __kmp_affinity_create_cpuinfo_map()
// __kmp_affinity_create_x2apicid_map().
//
inline static bool
__kmp_affinity_uniform_topology()
{
return __kmp_avail_proc == (__kmp_nThreadsPerCore * nCoresPerPkg * nPackages);
}
//
// Print out the detailed machine topology map, i.e. the physical locations
// of each OS proc.
//
static void
__kmp_affinity_print_topology(AddrUnsPair *address2os, int len, int depth,
int pkgLevel, int coreLevel, int threadLevel)
{
int proc;
KMP_INFORM(OSProcToPhysicalThreadMap, "KMP_AFFINITY");
for (proc = 0; proc < len; proc++) {
int level;
kmp_str_buf_t buf;
__kmp_str_buf_init(&buf);
for (level = 0; level < depth; level++) {
if (level == threadLevel) {
__kmp_str_buf_print(&buf, "%s ", KMP_I18N_STR(Thread));
}
else if (level == coreLevel) {
__kmp_str_buf_print(&buf, "%s ", KMP_I18N_STR(Core));
}
else if (level == pkgLevel) {
__kmp_str_buf_print(&buf, "%s ", KMP_I18N_STR(Package));
}
else if (level > pkgLevel) {
__kmp_str_buf_print(&buf, "%s_%d ", KMP_I18N_STR(Node),
level - pkgLevel - 1);
}
else {
__kmp_str_buf_print(&buf, "L%d ", level);
}
__kmp_str_buf_print(&buf, "%d ",
address2os[proc].first.labels[level]);
}
KMP_INFORM(OSProcMapToPack, "KMP_AFFINITY", address2os[proc].second,
buf.str);
__kmp_str_buf_free(&buf);
}
}
#if KMP_USE_HWLOC
static int
__kmp_affinity_create_hwloc_map(AddrUnsPair **address2os,
kmp_i18n_id_t *const msg_id)
{
*address2os = NULL;
*msg_id = kmp_i18n_null;
//
// Save the affinity mask for the current thread.
//
kmp_affin_mask_t *oldMask;
KMP_CPU_ALLOC(oldMask);
__kmp_get_system_affinity(oldMask, TRUE);
unsigned depth = hwloc_topology_get_depth(__kmp_hwloc_topology);
int threadLevel = hwloc_get_type_depth(__kmp_hwloc_topology, HWLOC_OBJ_PU);
int coreLevel = hwloc_get_type_depth(__kmp_hwloc_topology, HWLOC_OBJ_CORE);
int pkgLevel = hwloc_get_type_depth(__kmp_hwloc_topology, HWLOC_OBJ_SOCKET);
__kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 0;
//
// This makes an assumption about the topology being four levels:
// machines -> packages -> cores -> hardware threads
//
hwloc_obj_t current_level_iterator = hwloc_get_root_obj(__kmp_hwloc_topology);
hwloc_obj_t child_iterator;
for(child_iterator = hwloc_get_next_child(__kmp_hwloc_topology, current_level_iterator, NULL);
child_iterator != NULL;
child_iterator = hwloc_get_next_child(__kmp_hwloc_topology, current_level_iterator, child_iterator))
{
nPackages++;
}
current_level_iterator = hwloc_get_obj_by_depth(__kmp_hwloc_topology, pkgLevel, 0);
for(child_iterator = hwloc_get_next_child(__kmp_hwloc_topology, current_level_iterator, NULL);
child_iterator != NULL;
child_iterator = hwloc_get_next_child(__kmp_hwloc_topology, current_level_iterator, child_iterator))
{
nCoresPerPkg++;
}
current_level_iterator = hwloc_get_obj_by_depth(__kmp_hwloc_topology, coreLevel, 0);
for(child_iterator = hwloc_get_next_child(__kmp_hwloc_topology, current_level_iterator, NULL);
child_iterator != NULL;
child_iterator = hwloc_get_next_child(__kmp_hwloc_topology, current_level_iterator, child_iterator))
{
__kmp_nThreadsPerCore++;
}
if (! KMP_AFFINITY_CAPABLE())
{
//
// Hack to try and infer the machine topology using only the data
// available from cpuid on the current thread, and __kmp_xproc.
//
KMP_ASSERT(__kmp_affinity_type == affinity_none);
__kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
if (__kmp_affinity_verbose) {
KMP_INFORM(AffNotCapableUseLocCpuidL11, "KMP_AFFINITY");
KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
if (__kmp_affinity_uniform_topology()) {
KMP_INFORM(Uniform, "KMP_AFFINITY");
} else {
KMP_INFORM(NonUniform, "KMP_AFFINITY");
}
KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
__kmp_nThreadsPerCore, __kmp_ncores);
}
return 0;
}
//
// Allocate the data structure to be returned.
//
AddrUnsPair *retval = (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair) * __kmp_avail_proc);
unsigned num_hardware_threads = hwloc_get_nbobjs_by_depth(__kmp_hwloc_topology, threadLevel);
unsigned i;
hwloc_obj_t hardware_thread_iterator;
int nActiveThreads = 0;
for(i=0;i<num_hardware_threads;i++) {
hardware_thread_iterator = hwloc_get_obj_by_depth(__kmp_hwloc_topology, threadLevel, i);
Address addr(3);
if(! KMP_CPU_ISSET(i, fullMask)) continue;
addr.labels[0] = hardware_thread_iterator->parent->parent->logical_index;
addr.labels[1] = hardware_thread_iterator->parent->logical_index % nCoresPerPkg;
addr.labels[2] = hardware_thread_iterator->logical_index % __kmp_nThreadsPerCore;
retval[nActiveThreads] = AddrUnsPair(addr, hardware_thread_iterator->os_index);
nActiveThreads++;
}
//
// If there's only one thread context to bind to, return now.
//
KMP_ASSERT(nActiveThreads > 0);
if (nActiveThreads == 1) {
__kmp_ncores = nPackages = 1;
__kmp_nThreadsPerCore = nCoresPerPkg = 1;
if (__kmp_affinity_verbose) {
char buf[KMP_AFFIN_MASK_PRINT_LEN];
__kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
KMP_INFORM(AffUsingHwloc, "KMP_AFFINITY");
if (__kmp_affinity_respect_mask) {
KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
} else {
KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
}
KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
KMP_INFORM(Uniform, "KMP_AFFINITY");
KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
__kmp_nThreadsPerCore, __kmp_ncores);
}
if (__kmp_affinity_type == affinity_none) {
__kmp_free(retval);
KMP_CPU_FREE(oldMask);
return 0;
}
//
// Form an Address object which only includes the package level.
//
Address addr(1);
addr.labels[0] = retval[0].first.labels[pkgLevel-1];
retval[0].first = addr;
if (__kmp_affinity_gran_levels < 0) {
__kmp_affinity_gran_levels = 0;
}
if (__kmp_affinity_verbose) {
__kmp_affinity_print_topology(retval, 1, 1, 0, -1, -1);
}
*address2os = retval;
KMP_CPU_FREE(oldMask);
return 1;
}
//
// Sort the table by physical Id.
//
qsort(retval, nActiveThreads, sizeof(*retval), __kmp_affinity_cmp_Address_labels);
//
// When affinity is off, this routine will still be called to set
// __kmp_ncores, as well as __kmp_nThreadsPerCore,
// nCoresPerPkg, & nPackages. Make sure all these vars are set
// correctly, and return if affinity is not enabled.
//
__kmp_ncores = hwloc_get_nbobjs_by_depth(__kmp_hwloc_topology, coreLevel);
//
// Check to see if the machine topology is uniform
//
unsigned npackages = hwloc_get_nbobjs_by_depth(__kmp_hwloc_topology, pkgLevel);
unsigned ncores = __kmp_ncores;
unsigned nthreads = hwloc_get_nbobjs_by_depth(__kmp_hwloc_topology, threadLevel);
unsigned uniform = (npackages * nCoresPerPkg * __kmp_nThreadsPerCore == nthreads);
//
// Print the machine topology summary.
//
if (__kmp_affinity_verbose) {
char mask[KMP_AFFIN_MASK_PRINT_LEN];
__kmp_affinity_print_mask(mask, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
KMP_INFORM(AffUsingHwloc, "KMP_AFFINITY");
if (__kmp_affinity_respect_mask) {
KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", mask);
} else {
KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", mask);
}
KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
if (uniform) {
KMP_INFORM(Uniform, "KMP_AFFINITY");
} else {
KMP_INFORM(NonUniform, "KMP_AFFINITY");
}
kmp_str_buf_t buf;
__kmp_str_buf_init(&buf);
__kmp_str_buf_print(&buf, "%d", npackages);
//for (level = 1; level <= pkgLevel; level++) {
// __kmp_str_buf_print(&buf, " x %d", maxCt[level]);
// }
KMP_INFORM(TopologyExtra, "KMP_AFFINITY", buf.str, nCoresPerPkg,
__kmp_nThreadsPerCore, __kmp_ncores);
__kmp_str_buf_free(&buf);
}
if (__kmp_affinity_type == affinity_none) {
KMP_CPU_FREE(oldMask);
return 0;
}
//
// Find any levels with radiix 1, and remove them from the map
// (except for the package level).
//
int new_depth = 0;
int level;
unsigned proc;
for (level = 1; level < (int)depth; level++) {
if ((hwloc_get_nbobjs_by_depth(__kmp_hwloc_topology,level) == 1) && (level != pkgLevel)) {
continue;
}
new_depth++;
}
//
// If we are removing any levels, allocate a new vector to return,
// and copy the relevant information to it.
//
if (new_depth != depth-1) {
AddrUnsPair *new_retval = (AddrUnsPair *)__kmp_allocate(
sizeof(AddrUnsPair) * nActiveThreads);
for (proc = 0; (int)proc < nActiveThreads; proc++) {
Address addr(new_depth);
new_retval[proc] = AddrUnsPair(addr, retval[proc].second);
}
int new_level = 0;
for (level = 1; level < (int)depth; level++) {
if ((hwloc_get_nbobjs_by_depth(__kmp_hwloc_topology,level) == 1) && (level != pkgLevel)) {
if (level == threadLevel) {
threadLevel = -1;
}
else if ((threadLevel >= 0) && (level < threadLevel)) {
threadLevel--;
}
if (level == coreLevel) {
coreLevel = -1;
}
else if ((coreLevel >= 0) && (level < coreLevel)) {
coreLevel--;
}
if (level < pkgLevel) {
pkgLevel--;
}
continue;
}
for (proc = 0; (int)proc < nActiveThreads; proc++) {
new_retval[proc].first.labels[new_level]
= retval[proc].first.labels[level];
}
new_level++;
}
__kmp_free(retval);
retval = new_retval;
depth = new_depth;
}
if (__kmp_affinity_gran_levels < 0) {
//
// Set the granularity level based on what levels are modeled
// in the machine topology map.
//
__kmp_affinity_gran_levels = 0;
if ((threadLevel-1 >= 0) && (__kmp_affinity_gran > affinity_gran_thread)) {
__kmp_affinity_gran_levels++;
}
if ((coreLevel-1 >= 0) && (__kmp_affinity_gran > affinity_gran_core)) {
__kmp_affinity_gran_levels++;
}
if (__kmp_affinity_gran > affinity_gran_package) {
__kmp_affinity_gran_levels++;
}
}
if (__kmp_affinity_verbose) {
__kmp_affinity_print_topology(retval, nActiveThreads, depth-1, pkgLevel-1,
coreLevel-1, threadLevel-1);
}
KMP_CPU_FREE(oldMask);
*address2os = retval;
if(depth == 0) return 0;
else return depth-1;
}
#endif // KMP_USE_HWLOC
//
// If we don't know how to retrieve the machine's processor topology, or
// encounter an error in doing so, this routine is called to form a "flat"
// mapping of os thread id's <-> processor id's.
//
static int
__kmp_affinity_create_flat_map(AddrUnsPair **address2os,
kmp_i18n_id_t *const msg_id)
{
*address2os = NULL;
*msg_id = kmp_i18n_null;
//
// Even if __kmp_affinity_type == affinity_none, this routine might still
// called to set __kmp_ncores, as well as
// __kmp_nThreadsPerCore, nCoresPerPkg, & nPackages.
//
if (! KMP_AFFINITY_CAPABLE()) {
KMP_ASSERT(__kmp_affinity_type == affinity_none);
__kmp_ncores = nPackages = __kmp_xproc;
__kmp_nThreadsPerCore = nCoresPerPkg = 1;
if (__kmp_affinity_verbose) {
KMP_INFORM(AffFlatTopology, "KMP_AFFINITY");
KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
KMP_INFORM(Uniform, "KMP_AFFINITY");
KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
__kmp_nThreadsPerCore, __kmp_ncores);
}
return 0;
}
//
// When affinity is off, this routine will still be called to set
// __kmp_ncores, as well as __kmp_nThreadsPerCore,
// nCoresPerPkg, & nPackages. Make sure all these vars are set
// correctly, and return now if affinity is not enabled.
//
__kmp_ncores = nPackages = __kmp_avail_proc;
__kmp_nThreadsPerCore = nCoresPerPkg = 1;
if (__kmp_affinity_verbose) {
char buf[KMP_AFFIN_MASK_PRINT_LEN];
__kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, fullMask);
KMP_INFORM(AffCapableUseFlat, "KMP_AFFINITY");
if (__kmp_affinity_respect_mask) {
KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
} else {
KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
}
KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
KMP_INFORM(Uniform, "KMP_AFFINITY");
KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
__kmp_nThreadsPerCore, __kmp_ncores);
}
if (__kmp_affinity_type == affinity_none) {
return 0;
}
//
// Contruct the data structure to be returned.
//
*address2os = (AddrUnsPair*)
__kmp_allocate(sizeof(**address2os) * __kmp_avail_proc);
int avail_ct = 0;
unsigned int i;
KMP_CPU_SET_ITERATE(i, fullMask) {
//
// Skip this proc if it is not included in the machine model.
//
if (! KMP_CPU_ISSET(i, fullMask)) {
continue;
}
Address addr(1);
addr.labels[0] = i;
(*address2os)[avail_ct++] = AddrUnsPair(addr,i);
}
if (__kmp_affinity_verbose) {
KMP_INFORM(OSProcToPackage, "KMP_AFFINITY");
}
if (__kmp_affinity_gran_levels < 0) {
//
// Only the package level is modeled in the machine topology map,
// so the #levels of granularity is either 0 or 1.
//
if (__kmp_affinity_gran > affinity_gran_package) {
__kmp_affinity_gran_levels = 1;
}
else {
__kmp_affinity_gran_levels = 0;
}
}
return 1;
}
# if KMP_GROUP_AFFINITY
//
// If multiple Windows* OS processor groups exist, we can create a 2-level
// topology map with the groups at level 0 and the individual procs at
// level 1.
//
// This facilitates letting the threads float among all procs in a group,
// if granularity=group (the default when there are multiple groups).
//
static int
__kmp_affinity_create_proc_group_map(AddrUnsPair **address2os,
kmp_i18n_id_t *const msg_id)
{
*address2os = NULL;
*msg_id = kmp_i18n_null;
//
// If we don't have multiple processor groups, return now.
// The flat mapping will be used.
//
if ((! KMP_AFFINITY_CAPABLE()) || (__kmp_get_proc_group(fullMask) >= 0)) {
// FIXME set *msg_id
return -1;
}
//
// Contruct the data structure to be returned.
//
*address2os = (AddrUnsPair*)
__kmp_allocate(sizeof(**address2os) * __kmp_avail_proc);
int avail_ct = 0;
int i;
KMP_CPU_SET_ITERATE(i, fullMask) {
//
// Skip this proc if it is not included in the machine model.
//
if (! KMP_CPU_ISSET(i, fullMask)) {
continue;
}
Address addr(2);
addr.labels[0] = i / (CHAR_BIT * sizeof(DWORD_PTR));
addr.labels[1] = i % (CHAR_BIT * sizeof(DWORD_PTR));
(*address2os)[avail_ct++] = AddrUnsPair(addr,i);
if (__kmp_affinity_verbose) {
KMP_INFORM(AffOSProcToGroup, "KMP_AFFINITY", i, addr.labels[0],
addr.labels[1]);
}
}
if (__kmp_affinity_gran_levels < 0) {
if (__kmp_affinity_gran == affinity_gran_group) {
__kmp_affinity_gran_levels = 1;
}
else if ((__kmp_affinity_gran == affinity_gran_fine)
|| (__kmp_affinity_gran == affinity_gran_thread)) {
__kmp_affinity_gran_levels = 0;
}
else {
const char *gran_str = NULL;
if (__kmp_affinity_gran == affinity_gran_core) {
gran_str = "core";
}
else if (__kmp_affinity_gran == affinity_gran_package) {
gran_str = "package";
}
else if (__kmp_affinity_gran == affinity_gran_node) {
gran_str = "node";
}
else {
KMP_ASSERT(0);
}
// Warning: can't use affinity granularity \"gran\" with group topology method, using "thread"
__kmp_affinity_gran_levels = 0;
}
}
return 2;
}
# endif /* KMP_GROUP_AFFINITY */
# if KMP_ARCH_X86 || KMP_ARCH_X86_64
static int
__kmp_cpuid_mask_width(int count) {
int r = 0;
while((1<<r) < count)
++r;
return r;
}
class apicThreadInfo {
public:
unsigned osId; // param to __kmp_affinity_bind_thread
unsigned apicId; // from cpuid after binding
unsigned maxCoresPerPkg; // ""
unsigned maxThreadsPerPkg; // ""
unsigned pkgId; // inferred from above values
unsigned coreId; // ""
unsigned threadId; // ""
};
static int
__kmp_affinity_cmp_apicThreadInfo_os_id(const void *a, const void *b)
{
const apicThreadInfo *aa = (const apicThreadInfo *)a;
const apicThreadInfo *bb = (const apicThreadInfo *)b;
if (aa->osId < bb->osId) return -1;
if (aa->osId > bb->osId) return 1;
return 0;
}
static int
__kmp_affinity_cmp_apicThreadInfo_phys_id(const void *a, const void *b)
{
const apicThreadInfo *aa = (const apicThreadInfo *)a;
const apicThreadInfo *bb = (const apicThreadInfo *)b;
if (aa->pkgId < bb->pkgId) return -1;
if (aa->pkgId > bb->pkgId) return 1;
if (aa->coreId < bb->coreId) return -1;
if (aa->coreId > bb->coreId) return 1;
if (aa->threadId < bb->threadId) return -1;
if (aa->threadId > bb->threadId) return 1;
return 0;
}
//
// On IA-32 architecture and Intel(R) 64 architecture, we attempt to use
// an algorithm which cycles through the available os threads, setting
// the current thread's affinity mask to that thread, and then retrieves
// the Apic Id for each thread context using the cpuid instruction.
//
static int
__kmp_affinity_create_apicid_map(AddrUnsPair **address2os,
kmp_i18n_id_t *const msg_id)
{
kmp_cpuid buf;
int rc;
*address2os = NULL;
*msg_id = kmp_i18n_null;
//
// Check if cpuid leaf 4 is supported.
//
__kmp_x86_cpuid(0, 0, &buf);
if (buf.eax < 4) {
*msg_id = kmp_i18n_str_NoLeaf4Support;
return -1;
}
//
// The algorithm used starts by setting the affinity to each available
// thread and retrieving info from the cpuid instruction, so if we are
// not capable of calling __kmp_get_system_affinity() and
// _kmp_get_system_affinity(), then we need to do something else - use
// the defaults that we calculated from issuing cpuid without binding
// to each proc.
//
if (! KMP_AFFINITY_CAPABLE()) {
//
// Hack to try and infer the machine topology using only the data
// available from cpuid on the current thread, and __kmp_xproc.
//
KMP_ASSERT(__kmp_affinity_type == affinity_none);
//
// Get an upper bound on the number of threads per package using
// cpuid(1).
//
// On some OS/chps combinations where HT is supported by the chip
// but is disabled, this value will be 2 on a single core chip.
// Usually, it will be 2 if HT is enabled and 1 if HT is disabled.
//
__kmp_x86_cpuid(1, 0, &buf);
int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
if (maxThreadsPerPkg == 0) {
maxThreadsPerPkg = 1;
}
//
// The num cores per pkg comes from cpuid(4).
// 1 must be added to the encoded value.
//
// The author of cpu_count.cpp treated this only an upper bound
// on the number of cores, but I haven't seen any cases where it
// was greater than the actual number of cores, so we will treat
// it as exact in this block of code.
//
// First, we need to check if cpuid(4) is supported on this chip.
// To see if cpuid(n) is supported, issue cpuid(0) and check if eax
// has the value n or greater.
//
__kmp_x86_cpuid(0, 0, &buf);
if (buf.eax >= 4) {
__kmp_x86_cpuid(4, 0, &buf);
nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
}
else {
nCoresPerPkg = 1;
}
//
// There is no way to reliably tell if HT is enabled without issuing
// the cpuid instruction from every thread, can correlating the cpuid
// info, so if the machine is not affinity capable, we assume that HT
// is off. We have seen quite a few machines where maxThreadsPerPkg
// is 2, yet the machine does not support HT.
//
// - Older OSes are usually found on machines with older chips, which
// do not support HT.
//
// - The performance penalty for mistakenly identifying a machine as
// HT when it isn't (which results in blocktime being incorrecly set
// to 0) is greater than the penalty when for mistakenly identifying
// a machine as being 1 thread/core when it is really HT enabled
// (which results in blocktime being incorrectly set to a positive
// value).
//
__kmp_ncores = __kmp_xproc;
nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
__kmp_nThreadsPerCore = 1;
if (__kmp_affinity_verbose) {
KMP_INFORM(AffNotCapableUseLocCpuid, "KMP_AFFINITY");
KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
if (__kmp_affinity_uniform_topology()) {
KMP_INFORM(Uniform, "KMP_AFFINITY");
} else {
KMP_INFORM(NonUniform, "KMP_AFFINITY");
}
KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
__kmp_nThreadsPerCore, __kmp_ncores);
}
return 0;
}
//
//
// From here on, we can assume that it is safe to call
// __kmp_get_system_affinity() and __kmp_set_system_affinity(),
// even if __kmp_affinity_type = affinity_none.
//
//
// Save the affinity mask for the current thread.
//
kmp_affin_mask_t *oldMask;
KMP_CPU_ALLOC(oldMask);
KMP_ASSERT(oldMask != NULL);
__kmp_get_system_affinity(oldMask, TRUE);
//
// Run through each of the available contexts, binding the current thread
// to it, and obtaining the pertinent information using the cpuid instr.
//
// The relevant information is:
//
// Apic Id: Bits 24:31 of ebx after issuing cpuid(1) - each thread context
// has a uniqie Apic Id, which is of the form pkg# : core# : thread#.
//
// Max Threads Per Pkg: Bits 16:23 of ebx after issuing cpuid(1). The
// value of this field determines the width of the core# + thread#
// fields in the Apic Id. It is also an upper bound on the number
// of threads per package, but it has been verified that situations
// happen were it is not exact. In particular, on certain OS/chip
// combinations where Intel(R) Hyper-Threading Technology is supported
// by the chip but has
// been disabled, the value of this field will be 2 (for a single core
// chip). On other OS/chip combinations supporting
// Intel(R) Hyper-Threading Technology, the value of
// this field will be 1 when Intel(R) Hyper-Threading Technology is
// disabled and 2 when it is enabled.
//
// Max Cores Per Pkg: Bits 26:31 of eax after issuing cpuid(4). The
// value of this field (+1) determines the width of the core# field in
// the Apic Id. The comments in "cpucount.cpp" say that this value is
// an upper bound, but the IA-32 architecture manual says that it is
// exactly the number of cores per package, and I haven't seen any
// case where it wasn't.
//
// From this information, deduce the package Id, core Id, and thread Id,
// and set the corresponding fields in the apicThreadInfo struct.
//
unsigned i;
apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
__kmp_avail_proc * sizeof(apicThreadInfo));
unsigned nApics = 0;
KMP_CPU_SET_ITERATE(i, fullMask) {
//
// Skip this proc if it is not included in the machine model.
//
if (! KMP_CPU_ISSET(i, fullMask)) {
continue;
}
KMP_DEBUG_ASSERT((int)nApics < __kmp_avail_proc);
__kmp_affinity_bind_thread(i);
threadInfo[nApics].osId = i;